﻿<?php

if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}

class plugin_iijoin_forum_n {
	function  __construct() {
		loadcache('plugin');
		global $_G;
		$this->vars = $_G['cache']['plugin']['iijoin_forum_n'];
		// var_dump($this->vars);die;
		$this->open=$this->vars['open'];
		if($this->open){
			// 自定义标题
			$this->mytitle= explode("/hhf/",str_replace(array("\r\n", "\n", "\r"), '/hhf/',$this->vars['mytitle']));
			//禁止板块
			$this->forums =unserialize($this->vars['forums']);
			//缓存时间
			$this->cache=intval($this->vars['cache']);
			//热图周期
			$this->hotpicdays=intval($this->vars['hotpicdays']);
			//热图推送
			$this->hotpic=explode(",",$this->vars['hotpic']);
			//列表样式
			$this->liststyle=intval($this->vars['liststyle']);
			//会员排行（删除）
			// $this->userstyle=intval($this->vars['userstyle']);
			//右侧标题栏背景色
			$this->color=trim($this->vars['color']);
			if(!$this->color) $this->color='#ECEDEF';

			//板块宽高
			$this->bw_h = $this->vars['bw_h'];
			$w = 1000;
			$h = 292;
			if($this->bw_h){
				$arr = explode("*", $this->bw_h);
				if(count($arr)==2){
					$w = $arr[0];
					$h = $arr[1];
				}
			}
			$this->w = $w;
			$this->h = $h;
			// 计算幻灯片宽高
			$pw = ceil((450/270) * $this->h);
			$this->pw = $pw;

			
		}
	}
	function shortlongdata($data){
		if($data>10000) $data=intval($data/10000).'万';
		else if($data>1000) $data=intval($data/1000).'千';	
		return $data;
	}
	
	function loadcachefile($tag){
		$filepath=DISCUZ_ROOT.'./data/sysdata/cache_iijoin_forum_n_'.$tag.'.php';	
		return $filepath;
	}
	
	/**
	 * 论坛新帖
	 * @return [type] [description]
	 */
	function ii_newthread(){
		global $_G;
 		$filepath=$this->loadcachefile('newthread');
		if($filepath&&file_exists($filepath)){
			@include_once $filepath;
		}
		if(time()-intval($lasttime)>$this->cache){//缓存过期 或者 无缓存
			if($this->forums[0]=='') unset($this->forums[0]);
			$notin='';
			if(count($this->forums)){
				$notin=' and fid not in('.implode(',',$this->forums).')';
			}

			/*
			1=标题+作者
			2=标题+发布时间
			3=标题+最后回复时间
			4=标题+浏览数+回复数
			*/				
			$querys=DB::query("SELECT tid,subject,author,authorid,dateline,lastpost,replies,views FROM ".DB::table('forum_thread')." where displayorder>=0 $notin order by dateline DESC LIMIT 0,10");
			$i=1;
			$newthread='';
			while($value=DB::fetch($querys)){
				$class=$i<4? ' class="topRank"':'';
				if($this->liststyle==1) $end='<span><a href="home.php?mod=space&uid='.$value['authorid'].'" target="_blank">'.cutstr($value['author'],8,'').'</a></span>';
				if($this->liststyle==2) $end='<span>'.date('Y-m-d',$value['dateline']).'</span>';
				if($this->liststyle==3) $end='<span><a href="forum.php?mod=redirect&tid='.$value['tid'].'&goto=lastpost#lastpost" target="_blank">'.date('Y-m-d',$value['lastpost']).'</a></span>';
				if($this->liststyle==4){
					$value['replies']=$this->shortlongdata($value['replies']);
					$value['views']=$this->shortlongdata($value['views']);				
					$end='<span>('.$value['replies'].'/'.$value['views'].')</span>';
				}
				$newthread.='<li'.$class.'>
				<em>'.$i.'</em><a href="forum.php?mod=viewthread&tid='.$value['tid'].'" title="'.$value['subject'].'" target="_blank">'. cutstr($value['subject'],50,'...').'</a>'.$end.'
			</li>';
				$i++;
			}
			$newthread=base64_encode($newthread);			
			if($newthread&&$this->cache){
				// echo libfile('function/cache');
				@require_once libfile('function/cache');
				$cacheArray .= "\$newthread='".$newthread."';\n\$lasttime=".time().";\n";
				writetocache('iijoin_forum_n_newthread', $cacheArray);				
			}
		}
		
		return base64_decode($newthread);
	}
	
	function ii_newreplies(){
		global $_G;
		$filepath=$this->loadcachefile('newreplies');
 		if($filepath&&file_exists($filepath)){
			@include_once $filepath;
		}
		if(time()-intval($lasttime)>$this->cache){//缓存过期 或者 无缓存
			if($this->forums[0]=='') unset($this->forums[0]);
			$notin='';
			if(count($this->forums)){
				$notin=' and fid not in('.implode(',',$this->forums).')';
			}
			$dateline=strtotime(date('Y-m-d',time()));
			/*
			1=标题+作者
			2=标题+发布时间
			3=标题+最后回复时间
			4=标题+浏览数+回复数
			*/				
			$querys=DB::query("SELECT tid,subject,author,authorid,dateline,lastpost,replies,views FROM ".DB::table('forum_thread')." where replies>0 and displayorder>=0 $notin order by lastpost DESC LIMIT 0,10");
			$i=1;
			$newreplies='';
			while($value=DB::fetch($querys)){
				$class=$i<4? ' class="topRank"':'';
				if($this->liststyle==1) $end='<span><a href="home.php?mod=space&uid='.$value['authorid'].'" target="_blank">'.cutstr($value['author'],8,'').'</a></span>';
				if($this->liststyle==2) $end='<span>'.date('Y-m-d',$value['dateline']).'</span>';
				if($this->liststyle==3) $end='<span><a href="forum.php?mod=redirect&tid='.$value['tid'].'&goto=lastpost#lastpost" target="_blank">'.date('Y-m-d',$value['lastpost']).'</a></span>';
				if($this->liststyle==4){
					$value['replies']=$this->shortlongdata($value['replies']);
					$value['views']=$this->shortlongdata($value['views']);				
					$end='<span>('.$value['replies'].'/'.$value['views'].')</span>';
				}
				$newreplies.='<li'.$class.'>
				<em>'.$i.'</em><a href="forum.php?mod=viewthread&tid='.$value['tid'].'" title="'.$value['subject'].'" target="_blank">'. cutstr($value['subject'],50,'...').'</a>'.$end.'
			</li>';
				$i++;
			}
			$newreplies=base64_encode($newreplies);	

			if($newreplies&&$this->cache){
				@require_once libfile('function/cache');
				$cacheArray .= "\$newreplies='".$newreplies."';\n\$lasttime=".time().";\n";

				writetocache('iijoin_forum_n_newreplies', $cacheArray);				
			}
		}
		return base64_decode($newreplies);
	}
	
	function ii_piclist(){
		global $_G;
		$filepath=$this->loadcachefile('piclist');

 		if($filepath&&file_exists($filepath)){
			@include_once $filepath;
		}
		if(time()-intval($lasttime)>$this->cache){//缓存过期 或者 无缓存

			$num=5;
			$piclist='';
			
			if($this->hotpic){
				foreach($this->hotpic as $k=>$tid){
					$tid=intval($tid);
					if($tid){
						$query = DB::query("SELECT a.tid,a.subject,b.attachment from ".DB::table('forum_thread')." as a left join ".DB::table('forum_threadimage')." as b on a.tid=b.tid where a.tid=$tid and a.attachment>0 and a.displayorder>=0 and b.attachment!='' order by a.views DESC LIMIT 0,1");
						$pic=DB::fetch($query);
						if($pic){
							$piclist.='<li style="width:'.$this->pw.';height:'.$this->h.';display: block;"><a href="forum.php?mod=viewthread&tid='.$pic['tid'].'" target="_blank"><img src="data/attachment/forum/'.$pic['attachment'].'" width="'.$this->pw.'" height="'.$this->h.'"></a><span class="title">'.$pic['subject'].'</span></li>';
							$num-=1;
						}
					}
				}	
			}
			if($num>0){
				$dateline=time()-86400*$this->hotpicdays;
				if($this->forums[0]=='') unset($this->forums[0]);
				$notin='';
				if(count($this->forums)){
					$notin=' and a.fid not in('.implode(',',$this->forums).')';
				}			
				$query = DB::query("SELECT a.tid,a.subject,b.attachment from ".DB::table('forum_thread')." as a left join ".DB::table('forum_threadimage')." as b on a.tid=b.tid where a.attachment>0 and a.displayorder>=0 and b.attachment!='' and a.dateline>".$dateline." $notin order by a.views DESC LIMIT 0,$num");
				while($pic=DB::fetch($query)){
					if($pic){

						$piclist.='<li style="width:'.$this->pw.';height:'.$this->h.';display: block;"><a href="forum.php?mod=viewthread&tid='.$pic['tid'].'" target="_blank"><img src="data/attachment/forum/'.$pic['attachment'].'" width="'.$this->pw.'" height="'.$this->h.'"></a><span class="title">'.$pic['subject'].'</span></li>';
					}
				}
			}
			
			$piclist=base64_encode($piclist);

			if($piclist&&$this->cache){
				@require_once libfile('function/cache');
				$cacheArray .= "\$piclist='".$piclist."';\n\$lasttime=".time().";\n";
				writetocache('iijoin_forum_n_piclist', $cacheArray);				
			}		
		}
		return base64_decode($piclist);
	}
	
	function ii_hotthread_by_days($days){
		global $_G;
		$filepath=$this->loadcachefile('hotthread_'.$days);
 		if($filepath&&file_exists($filepath)){
			@include_once $filepath;
		}
		if(time()-intval($lasttime)>$this->cache){//缓存过期 或者 无缓存
			if($this->forums[0]=='') unset($this->forums[0]);
			$notin='';
			if(count($this->forums)){
				$notin=' and fid not in('.implode(',',$this->forums).')';
			}
			$dateline=time()-$days*86400;
			/*
			1=标题+作者
			2=标题+发布时间
			3=标题+最后回复时间
			4=标题+浏览数+回复数
			*/				
			$querys=DB::query("SELECT tid,subject,author,authorid,dateline,lastpost,replies,views FROM ".DB::table('forum_thread')." where displayorder>=0 and dateline>".$dateline." $notin order by views DESC LIMIT 0,10");
			$i=1;
			$hotthread='';
			while($value=DB::fetch($querys)){
				$class=$i<4? ' class="topRank"':'';
				if($this->liststyle==1) $end='<span><a href="home.php?mod=space&uid='.$value['authorid'].'" target="_blank">'.cutstr($value['author'],8,'').'</a></span>';
				if($this->liststyle==2) $end='<span>'.date('Y-m-d',$value['dateline']).'</span>';
				if($this->liststyle==3) $end='<span><a href="forum.php?mod=redirect&tid='.$value['tid'].'&goto=lastpost#lastpost" target="_blank">'.date('Y-m-d',$value['lastpost']).'</a></span>';
				if($this->liststyle==4){
					$value['replies']=$this->shortlongdata($value['replies']);
					$value['views']=$this->shortlongdata($value['views']);				
					$end='<span>('.$value['replies'].'/'.$value['views'].')</span>';
				}
				$hotthread.='<li'.$class.'>
				<em>'.$i.'</em><a href="forum.php?mod=viewthread&tid='.$value['tid'].'" title="'.$value['subject'].'" target="_blank">'. cutstr($value['subject'],50,'...').'</a>'.$end.'
			</li>';
				$i++;
			}
			$hotthread=base64_encode($hotthread);			
			if($hotthread&&$this->cache){
				@require_once libfile('function/cache');
				$cacheArray .= "\$hotthread='".$hotthread."';\n\$lasttime=".time().";\n";
				writetocache('iijoin_forum_n_hotthread_'.$days, $cacheArray);				
			}
		}
		return base64_decode($hotthread);
	}
	
	function ii_newdigest(){
		global $_G;
		$filepath=$this->loadcachefile('newdigest');
 		if($filepath&&file_exists($filepath)){
			@include_once $filepath;
		}
		if(time()-intval($lasttime)>$this->cache){//缓存过期 或者 无缓存
			if($this->forums[0]=='') unset($this->forums[0]);
			$notin='';
			if(count($this->forums)){
				$notin=' and fid not in('.implode(',',$this->forums).')';
			}
			$dateline=strtotime(date('Y-m-d',time()));
			/*
			1=标题+作者
			2=标题+发布时间
			3=标题+最后回复时间
			4=标题+浏览数+回复数
			*/				
			$querys=DB::query("SELECT tid,subject,author,authorid,dateline,lastpost,replies,views FROM ".DB::table('forum_thread')." where digest!=0 and displayorder>=0 $notin order by dateline DESC LIMIT 0,10");
			$i=1;
			$newdigest='';
			while($value=DB::fetch($querys)){
				$class=$i<4? ' class="topRank"':'';
				if($this->liststyle==1) $end='<span><a href="home.php?mod=space&uid='.$value['authorid'].'" target="_blank">'.cutstr($value['author'],8,'').'</a></span>';
				if($this->liststyle==2) $end='<span>'.date('Y-m-d',$value['dateline']).'</span>';
				if($this->liststyle==3) $end='<span><a href="forum.php?mod=redirect&tid='.$value['tid'].'&goto=lastpost#lastpost" target="_blank">'.date('Y-m-d',$value['lastpost']).'</a></span>';
				if($this->liststyle==4){
					$value['replies']=$this->shortlongdata($value['replies']);
					$value['views']=$this->shortlongdata($value['views']);				
					$end='<span>('.$value['replies'].'/'.$value['views'].')</span>';
				}
				$newdigest.='<li'.$class.'>
				<em>'.$i.'</em><a href="forum.php?mod=viewthread&tid='.$value['tid'].'" title="'.$value['subject'].'" target="_blank">'. cutstr($value['subject'],50,'...').'</a>'.$end.'
			</li>';
				$i++;
			}
			$newdigest=base64_encode($newdigest);			
			if($newdigest&&$this->cache){
				@require_once libfile('function/cache');
				$cacheArray .= "\$newdigest='".$newdigest."';\n\$lasttime=".time().";\n";
				writetocache('iijoin_forum_n_newdigest', $cacheArray);				
			}
		}
		return base64_decode($newdigest);
	}
	
	
}
class plugin_iijoin_forum_n_forum extends plugin_iijoin_forum_n{
	function index_top() {

		global $_G;
		$ext = 'px';
		// 容器宽高
		$w = $this->w.$ext;
		$h = $this->h.$ext;
		// 图片宽
		$pw = $this->pw.$ext;

		// 内容列表宽
		$listw = ($w - $pw).$ext;
		
		$newthread=$this->ii_newthread();//论坛新帖
		$newreplies=$this->ii_newreplies();//最新回复
		$piclist=$this->ii_piclist();//幻灯片
		$hotthread7=$this->ii_hotthread_by_days(7);//本周热点
		$hotthread30=$this->ii_hotthread_by_days(30);//本月热点
		$newdigest=$this->ii_newdigest();//论坛精华
		
		foreach($this->mytitle as $k=>$item){
 			$name=explode('=',$item);
			$name[0]=intval($name[0]);
			$name[1]=trim($name[1]);
			if($name[0]&&$name[1]) $itemname[$name[0]]=$name[1];
		}
		include template('iijoin_forum_n:ii_forum');
		return $return;
	}
}

?>